/**
 * Created by Think on 2016/4/12.
 */
public class Complex{// 1. Z = a(实部) + bi（虚部）;

    private int RealPart;	// 实部
    private int ImaginPart;	// 虚部

    public int getRealPart() {
        return RealPart;
    }

    public void setRealPart(int r) {
        RealPart = r;
    }

    public int getImaginPart() {
        return ImaginPart;
    }

    public void setImaginPart(int i) {
        ImaginPart = i;
    }
    // 构造函数
    public Complex() {}
    public Complex(int r, int i) {
        super();
        RealPart = r;
        ImaginPart = i;
    }

    // 相加
    public static String add(Complex a,Complex b){
        Complex temp = new Complex();
        temp.setRealPart(a.getRealPart()+b.getRealPart());
        temp.setImaginPart(a.getImaginPart()+b.getImaginPart());
        return show(temp);
    }

    // 相减
    public static String minus(Complex a,Complex b){
        Complex temp = new Complex();
        temp.setRealPart(a.getRealPart()-b.getRealPart());
        temp.setImaginPart(a.getImaginPart()-b.getImaginPart());
        return show(temp);
    }

    //相乘
//A*B=ac-bd+(ad+bc)i
//A=a+bi, B=c+di
    public static String Multi(Complex a,Complex b){
        Complex temp = new Complex();
        temp.setRealPart((a.getRealPart()*b.getRealPart())-(a.getImaginPart()*b.getImaginPart()));
        temp.setImaginPart((b.getImaginPart()*a.getRealPart())+(b.getRealPart()*a.getImaginPart()));
        return show(temp);
    }



    // 显示输出
    public static String show(Complex a){
        StringBuffer sb = new StringBuffer();
        sb.append(a.getRealPart());
        if(a.getImaginPart()>0){
            sb.append("+"+a.getImaginPart()+"i");
        }else if(a.getImaginPart()<0){
            sb.append(a.getImaginPart()+"i");
        }
        return sb.toString();
    }

}
